package com.suncnpap.base.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.app.util.common.Constants;
import com.app.util.common.HandlerResult;
import com.app.util.common.ObjUtil;
import com.app.util.common.UuidUtil;
import com.app.util.database.mybatis.Page;
import com.suncnpap.base.common.GlobaUser;
import com.suncnpap.base.common.Ticket;
import com.suncnpap.base.model.BaseObj;


/**
 * 
 *
 */
public class BaseController {
	
	protected Logger logger = Logger.getLogger(this.getClass());
	
	/**
	 * 初始化基本数据
	 * @param baseObj
	 */
	protected void init(BaseObj baseObj){
		baseObj.setStrId(UuidUtil.get32UUID());//主键ID
		baseObj.setDtCreatDate(new Date());//创建时间
		
		baseObj.setStrCreatId(GlobaUser.getStrId(getRequest().getHeader("token")));//创建人ID
		baseObj.setStrCreatName(GlobaUser.getStrCname(getRequest().getHeader("token")));//创建人姓名
		baseObj.setStrSchoolId(GlobaUser.getStrSchoolId(getRequest().getHeader("token")));//独立机构ID
	}
	
	public String get32UUID() {
		return UuidUtil.get32UUID();
	}
	
	
	
	/**得到request对象
	 * @return
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	
	
	/**
	 * 
	 * @param param
	 * @return
	 */
	public String getParameter(String param){
		return getRequest().getParameter(param);
	}
	public String getParameter(String param,String defaultStr){
		String value = getRequest().getParameter(param);
		return value == null||value.equals("") ?defaultStr :value;
	}
	public String[] getParameterArr(String param){
		return getRequest().getParameterValues(param);
	}
	
	public HandlerResult checkToken(HttpServletRequest request){
		HandlerResult result = new HandlerResult();
		
		String token = request.getHeader("token");
		List<Object> obj=Ticket.getTicketMap().get(token);
		System.out.println("--------"+obj);
		if(obj!=null){
			long nowTime=new Date().getTime();
			long oldTime=(Long)obj.get(2);
			if((nowTime-oldTime)>(24*60*60*1000)){//有效期 30分钟
//				Ticket.getTicketMap().remove(token);
				result.setCode(50012);
				result.setMessage("token过期");
			}else{
				result.put("strSchoolId", GlobaUser.getStrSchoolId(token));
			}
		}else{
			result.setCode(50008);
			result.setMessage("token失效");
		}
		
		return result;
	}
	
	public HandlerResult checkToken(MultipartHttpServletRequest request){
		HandlerResult result = new HandlerResult();
		
//		String token = request.getHeader("token");
//		List<Object> obj=Ticket.getTicketMap().get(token);
//		System.out.println(obj);
//		if(obj!=null){
//			long nowTime=new Date().getTime();
//			long oldTime=(Long)obj.get(2);
//			if((nowTime-oldTime)>(24*60*60*1000)){//有效期 30分钟
////				Ticket.getTicketMap().remove(token);
//				result.setCode(50012);
//				result.setMessage("token过期");
//			}
//		}else{
//			result.setCode(50008);
//			result.setMessage("token失效");
//		}
		
		return result;
	}
	
	public Page setPage(){
		Integer pageIndex = Integer.valueOf(this.getParameter("pageIndex","0"));
		Integer pageSize = Integer.valueOf(this.getParameter("pageSize","0"));
		String strSchoolId = this.getParameter("strSchoolId", GlobaUser.getStrSchoolId(this.getRequest().getHeader("token")));
		
		Page page = new Page();
		if(!strSchoolId.equals("") && !GlobaUser.getStrSchoolId(this.getRequest().getHeader("token")).equals("-1")){
			page.addQueryAnd("strSchoolId", strSchoolId);
		}
		page.setPage(pageIndex);
		
		
		if(pageIndex==-1||pageIndex==0){
			page.setUsePage(false);
		}else{
			page.setRows(pageSize);
		}
		
		
		return page;
	}
	
	public HandlerResult returnPage(HandlerResult result, Page page){
		
		result.put("pageIndex", page.getPage());
		result.put("pageSize", page.getRows());
		result.put("total", page.getTotalResult());
		result.put("list", page.getDataList());
		
		return result;
	}
}
